package com.wisemoresystem.seal.action.seal.use;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wisemoresystem.seal.base.action.ProjBaseAction;
import com.wisemoresystem.seal.model.t_seal;
import com.wisemoresystem.seal.model.t_seal_user;
import com.wisemoresystem.seal.service.SealSetService;
import com.wisemoresystem.seal.web.WebTools;

/**
 * 印章管理
 * 
 * @author Administrator
 *
 */
@Controller("ixSealGet")
@Scope("prototype")
@RequestMapping("/ix/seal/set/get")
public class SealGetAction extends ProjBaseAction {

	@Autowired
	private SealSetService sealService;
	@Autowired
	private SealSetService bService;

	/**
	 * 印章领用列表
	 * 
	 * @param srh_name
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(String srh_name) {

		List<Map<String, Object>> list = sealService.selectSealGetList(
				srh_name, page);
		List<Map<String, Object>> userlist = sealService.selectSealUserList();

		ModelAndView mv = new ModelAndView("/jsp/seal/set/get/list");
		mv.addObject("list", list);
		mv.addObject("userlist", userlist);
		mv.addObject("user_id", WebTools.getCurrentUserId());
		return mv;
	}

	/**
	 * 印章领用
	 * 
	 * @param seal_id
	 *            印章id
	 * @param sta
	 *            状态
	 * @param user_id
	 *            用户id
	 * @throws IOException
	 */
	@RequestMapping("/operate")
	public void operate(String seal_id, String sta, String user_id)
			throws IOException {
		resp.setCharacterEncoding("utf-8");

		t_seal sealDb = oc.findObjById(seal_id, t_seal.class);
		sealDb.setFlag_use(sta);
		sealDb.setFlag_use_status("未安装");

		if (sta.equals("已领取")) {
			sealDb.setRecieve_user_id(user_id);
			t_seal_user seal_userDb = new t_seal_user();
			seal_userDb.setFlag("已领取");
			seal_userDb.setUse_user_id(user_id);
			seal_userDb.setSeal_id(seal_id);
			seal_userDb.setUser_id(WebTools.getCurrentUserId());
			seal_userDb.setBank_id(WebTools.getCurrentBankId());
			seal_userDb.setTime_get(new Timestamp(System.currentTimeMillis()));
			oc.saveObj(seal_userDb);
			logT.logH("领用印章", t_seal.class, sealDb.get_id());
		} else if (sta.equals("未领取")) {
			sealDb.setRecieve_user_id("");
			t_seal_user seal_user = sealService.selectGetSealUser(seal_id);
			seal_user.setFlag("已归还");
			seal_user.setTime_back(new Timestamp(System.currentTimeMillis()));
			oc.updateObj(seal_user);
			logT.logH("归还印章", t_seal.class, sealDb.get_id());
		} else if (sta.equals("待归还")) {
			sealDb.setRecieve_user_id("");
			t_seal_user seal_user = sealService.selectGetSealUser(seal_id);
			seal_user.setFlag("已归还");
			seal_user.setTime_back(new Timestamp(System.currentTimeMillis()));
			oc.updateObj(seal_user);
			logT.logH("归还印章", t_seal.class, sealDb.get_id());
			sealDb.setFlag_use("未领取");
			sealDb.setFlag("停用");
			if (!bService.checkIsTop(seal_id)) {
				sealDb.setFlag_stop("上缴停用");
			}

			logT.logH("停用印章", t_seal.class, sealDb.get_id());
		} else {
			sealDb.setRecieve_user_id("");
			t_seal_user seal_user = sealService.selectGetSealUser(seal_id);
			seal_user.setFlag("已归还");
			seal_user.setTime_back(new Timestamp(System.currentTimeMillis()));
			oc.updateObj(seal_user);
			sealDb.setFlag_stop("上缴停用");
			sealDb.setFlag("停用");
			logT.logH("归还印章", t_seal.class, sealDb.get_id());
		}

		oc.updateObj(sealDb);

		resp.getWriter().print("success");
		resp.getWriter().flush();
	}

}
